package reg
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import mx.core.ByteArrayAsset;
	
	public class ChatFilter extends Sprite
	{
		[Embed(source="assets/words.txt", mimeType="application/octet-stream")]
		public static const embedWordsBanned:Class;
		
		private var arrBanned:Array;
		private var txt:TextField;
		
		public function ChatFilter()
		{
			super();
			init();
		}
		
		public function init():void {
			txt = new TextField();
			this.addChild(txt);
			txt.x = 100;
			txt.y = 100;
			txt.text = "hello";
			
			this.stage.addEventListener(MouseEvent.CLICK,onClick);
			var storyByteArray:ByteArrayAsset = ByteArrayAsset(new embedWordsBanned());
			var banWorld:String = storyByteArray.readUTFBytes(storyByteArray.length);
			arrBanned = banWorld.split("\r\n");
			
		}
		
		public function onClick(evt:MouseEvent):void {
			var content:String = txt.text;
			if(content!="")
			filter(content);
		}
		
		public function filter(content:String):void {
			for each( var element:String in arrBanned )
			{
				content = content.replace( new RegExp(element, "g"), "***" );
				if(content.indexOf("*")!=-1) {
					trace(content);
				}
			}
			this.txt.text = content;
			trace(content+"....");
		}
	}
}